package cn.iocoder.yudao.module.booking.service.courseinfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.booking.controller.admin.courseinfo.vo.CourseInfoPageReqVO;
import cn.iocoder.yudao.module.booking.controller.admin.courseinfo.vo.CourseInfoSaveReqVO;
import cn.iocoder.yudao.module.booking.dal.dataobject.courseinfo.CourseInfoDO;
import cn.iocoder.yudao.module.booking.dal.mysql.courseinfo.CourseInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.booking.enums.ErrorCodeConstants.COURSE_INFO_NOT_EXISTS;

/**
 * 课程信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CourseInfoServiceImpl implements CourseInfoService {

    @Resource
    private CourseInfoMapper courseInfoMapper;

    @Override
    public Long createCourseInfo(CourseInfoSaveReqVO createReqVO) {
        // 插入
        CourseInfoDO courseInfo = BeanUtils.toBean(createReqVO, CourseInfoDO.class);
        courseInfoMapper.insert(courseInfo);
        // 返回
        return courseInfo.getId();
    }

    @Override
    public void updateCourseInfo(CourseInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateCourseInfoExists(updateReqVO.getId());
        // 更新
        CourseInfoDO updateObj = BeanUtils.toBean(updateReqVO, CourseInfoDO.class);
        courseInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteCourseInfo(Long id) {
        // 校验存在
        validateCourseInfoExists(id);
        // 删除
        courseInfoMapper.deleteById(id);
    }

    private void validateCourseInfoExists(Long id) {
        if (courseInfoMapper.selectById(id) == null) {
            throw exception(COURSE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public CourseInfoDO getCourseInfo(Long id) {
        return courseInfoMapper.selectById(id);
    }

    @Override
    public PageResult<CourseInfoDO> getCourseInfoPage(CourseInfoPageReqVO pageReqVO) {
        return courseInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public Integer updateAvailableNumberById(Long id, Long availableNumber) {
        return courseInfoMapper.updateAvailableNumberById(id,availableNumber);
    }

}